package com.meitianhui.convenitenbuy.module.mine.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.mine.contract.CollectionContract;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.CollectedStoresBean;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.CollectionActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by amy on 2017/11/18.
 */

public class CollectionPresenter implements CollectionContract.Presenter {

    private CollectionContract.View mView;
    private CollectionActivity mContext;

    public CollectionPresenter(CollectionActivity activity, CollectionActivity context) {
        this.mView = activity;
        this.mContext = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getUserCollectionStore() {
        Map<String, String> hashmap = new HashMap<>();
        hashmap.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());

        MineApiDataSource.getInstance()
                .getUserCollectionStore(InfoProvider.getAppToken(mContext),
                        "stores.consumer.favoriteStoreList", new Gson().toJson(hashmap))
                .subscribe(new ResponseObserver<BaseResponse<CollectedStoresBean>>(mContext) {
                    @Override
                    public void onSuccess(BaseResponse<CollectedStoresBean> response) {
                        List<CollectedStoresBean.Stores> storesList = response.data.list;
                        if (storesList.size() != 0) {
                            // 有收藏门店 1、本地保存 2、显示收藏信息
                            mView.showCollection(response.data);
                        } else {
                            // 无收藏门店
                            mView.showEmpty();
                        }
                    }
                });
    }

    /**
     * 跟新收藏店铺信息
     */
    @Override
    public void deleteStoreCollect(String mCollectStoresId) {
        Map<String, String> hashmap = new HashMap<>();
        hashmap.put("stores_id", mCollectStoresId);
        hashmap.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());

        MineApiDataSource.getInstance()
                .collectStore(InfoProvider.getAppToken(mContext),
                        "stores.consumer.favoriteStoreCancel", new Gson().toJson(hashmap))
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.upDataSuccess();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

    @Override
    public void setDefaultStore(String stores_id) {
        Map<String, String> hashmap = new HashMap<>();
        hashmap.put("stores_id", stores_id);
        hashmap.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());
        hashmap.put("is_llm_stores", "Y");

        MineApiDataSource.getInstance()
                .collectStore(InfoProvider.getAppToken(mContext),
                        "stores.consumer.favoriteStoreEdit", new Gson().toJson(hashmap))
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.upDataSuccess();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.showError(msg);
                    }
                });
    }

}
